import { fileURLToPath, URL } from 'node:url'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ViteCompressionPlugin from 'vite-plugin-compression'
import importToCDN from 'vite-plugin-cdn-import'


// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    ViteCompressionPlugin({
      filter: /\.(js|css)$/i,   // 匹配js和css文件
      threshold: 1024,         // 超过1kb的文件会被压缩
      algorithm: 'gzip',       // 压缩算法
      ext: '.gz'
    }),
    importToCDN({
      modules: [
        {
          name: 'echarts',
          var: 'echarts',
          path: 'https://cdn.jsdelivr.net/npm/echarts@5.5.1/dist/echarts.min.js'
        },
        {
          name: 'highlight.js',
          var: 'highlight.js',
          path: 'https://cdn.bootcdn.net/ajax/libs/highlight.js/11.10.0/es/core.min.js'
        }
      ]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    }
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:8081',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      }
    }
  }
})
